<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author Меркулов Илья aka Bit0reZ
 * @license GNU GPLv3
 */

class Module extends CI_Controller {
	
	var $uid ; // UID in VVSU
        var $role ; // Role in VVSU
        var $maxParticipantCount = 60; // max users on Webinar
        var $base_url;
	var $env = ENVIRONMENT;
	var $stat;
	var $bbb_url;
	var $salt;

	function __construct()
	{
		parent::__construct();
                $this->load->helper('url');
                $this->load->helper('html');
                $this->load->database('default');
                $this->load->model('WebinarDB','wdb');
                $this->load->library('parser');
                $this->load->library('session');
                $this->load->library('BigBlueButton', NULL, 'bbb');
                $this->load->library('table');
		$this->load->config('eclass_cfg');
                $this->load->helper('data');
                $this->uid = $this->session->userdata('UID');
                $this->role = $this->session->userdata('role');
                $this->base_url = base_url();
		$this->bbb_url = $this->config->item('bbb_url');
		$this->salt = $this->config->item('salt');
	}

        function index()
        {
                show_404();
        }

        function mload($class,$pg)
        {
                if(isset($class) && isset($pg))
                {
                        $data['base_url'] = base_url();
                        $data['url'] = $this->bbb->url;
                        $data['salt'] = $this->bbb->salt;
                        $data['UID'] = $this->session->userdata('UID');
                        $data['role'] = $this->session->userdata('role');
                        $data['surname'] = $this->session->userdata('surname');
                        $data['firstname'] = $this->session->userdata('firstname');
                        $data['login'] = $data['surname']." ".$data['firstname'];
                        $this->parser->parse($class.'/'.$pg.'_tpl', $data);
                }
                else
                {
                        show_404();
                }
        }

        function gadget($name)
        {
                $post = $this->input->post('json');
                if($post) $data = json_decode ($post, TRUE);
                else $data = array();
                $data['base_url'] = base_url();
                $this->parser->parse('gadgets/'.$name, $data);
        }

        function tpl($name)
        {
                $post = $this->input->post('json');
                if($post) $data = json_decode($post, TRUE);
                else $data = array();
		if(isset($data['model'])){
			$data[$data['model']] = $this->wdb->$data['model']();
			unset($data['model']);
		}
                $data['base_url'] = base_url();
                $this->parser->parse('templates/'.$name.'.html', $data);
        }
	
	function jload($name)
	{
		header('Content-Type:text/javascript');
		$data['base_url'] = base_url();
                $this->parser->parse('js/'.$name.'.js', $data);
	}

	function gdata($name)
	{
		$param = $this->input->post('param');
		try{
			$this->load->library('gadgets/'.$name,'','gadget');
			$this->gadget->init($param);
			$gadget = $this->gadget->data();
			$this->output->set_output($gadget);
		}
		catch(Exception $e){ $this->output->set_output(json_encode(array('error' => 'Can`t load '+$name+'!'))); }
	}

        function gload($name)
	{
		$param = $this->input->post('param');
		try{
			$this->load->library('gadgets/'.$name,'','gadget');
			$this->gadget->init($param);
			$gadget = $this->gadget->load();
			$this->output->set_output($gadget);
		}
		catch(Exception $e){ $this->output->set_output(json_encode(array('error' => 'Can`t load '+$name+'!'))); }
        }
}
